package com.winsomely.leecode;
/**
 * 有效的完全平方数
 * 
 * @author liujiawei
 *
 */
public class IsPerfectSquare {

	public boolean isPerfectSquare1(long num) {
		if (num <= 1) {
			return true;
		}
		long ss = 0;
		long ys = 0;
		long L = num / 2 + 1;
		for (long i = 2; i <= L; i++) {
			ss = num / i;
			ys = num % i;
			if (ys == 0 && ss == i) {
				System.out.println(ss);
				return true;
			}
			if (ss < i) {
				return false;
			}
		}
		return false;
	}

	public boolean isPerfectSquare(long num) {
		if (num <= 1) {
			return true;
		}
		for (long i = 1;; i += 2) {
			num -= i;
			if (num == 0) {
				return true;
			}
			if (num < 0) {
				return false;
			}
		}
	}

	public static void main(String[] args) {
		
		
		IsPerfectSquare t = new IsPerfectSquare();
		long num = Long.MAX_VALUE;
		long t0 = System.currentTimeMillis();
		System.out.println(t.isPerfectSquare(num));
		long t1 = System.currentTimeMillis();
		
		System.out.println((t1 - t0));

		System.out.println(t.isPerfectSquare1(num));
		long t2 = System.currentTimeMillis();
		System.out.println((t2 - t1));
		
		
		long r = (long)Math.sqrt(num);
		long t3 = System.currentTimeMillis();
		System.out.println(r + " --- " + (t3 - t2));
	}

}
